/**
 * Created by Administrator on 2017/2/22.
 */
var body                    =  $('body'),
    login                   =  $('#login'),
    loginRegister                   =  $('#login_register'),
    loginAccounts           =  $('#login-accounts'),
    loginPassword           =  $('#login-password'),
    registerAccounts        =  $('#register-accounts'),
    registerPassword        =  $('#register-password'),
    registerNotPassword     =  $('#register-notpassword'),
    registerTel             =  $('#register-tel'),
    btnRegister             =  $('.btn-register'),
    rand;

rand = Math.floor(Math.random()*5) + 1;
body.css('background','url('+ThinkPHP['IMG']+'/bg' + rand + '.jpg) no-repeat center center fixed')
    .css('background-size','cover');

//登录对话框
login.dialog({
    title: '登录后台',
    width: 370,
    height:260,
    iconCls:'icon-lock',
    closed: false,
    modal : false,
    maximizable : false,
    closable : false,
    draggable : false,
    buttons:[{
        text:'登录',
        size:'large',
        id: 'login-btn',
        iconCls:'icon-go',
        handler:function(){
            if (login.form('validate')) {
                $.ajax({
                    url: ThinkPHP['MODULE'] + '/Login/checkUser',
                    type: 'POST',
                    data: {
                        'accounts': loginAccounts.val(),
                        'password': loginPassword.val()
                    },
                    beforeSend: function () {
                        $.messager.progress({
                            'text': '正在尝试登录中...'
                        });
                    },
                    success: function (data) {
                        $.messager.progress('close');
                        if (data > 0) {
                            location.href = ThinkPHP['INDEX'];
                        } else if (data == -1) {
                            $.messager.alert('登录操作', '登录帐号处于冻结状态', 'warning', function () {
                                loginAccounts.textbox('textbox').select();
                            });
                        } else if (data == 0 ) {
                            $.messager.alert('登录操作', '登录帐号或密码不正确', 'warning', function () {
                                loginAccounts.textbox('textbox').select();
                            });
                        }
                    }
                });
            }

        }
    }],
    onOpen : function () {
        $(function ()
        {
            $('#login-btn').parent().css('text-align', 'center');
        });
    }



});

//注册业务对话框
loginRegister.dialog({
    title: '注册业务',
    width: 370,
    height:300,
    iconCls:'icon-lock',
    closed: true,
    modal : false,
    maximizable : false,
    closable : true,
    draggable : false,
    buttons:[{
        text:'注册',
        size:'large',
        id: 'register-btn',
        iconCls:'icon-go',
        handler:function(){
            if (loginRegister.form('validate')) {
                $.ajax({
                    url: ThinkPHP['MODULE'] + '/Login/userRegister',
                    type: 'POST',
                    data: {
                        'accounts': registerAccounts.val(),
                        'password': registerPassword.val(),
                        'staff_id': 0,
                        'staff_name': '',
                        'notpassword': registerNotPassword.val(),
                        'state': '冻结',
                        'tel': registerTel.val(),
                        'not': '1'
                    },
                    beforeSend: function () {
                        $.messager.progress({
                            'text': '正在尝试注册中...'
                        });
                    },
                    success: function (data) {
                        $.messager.progress('close');
                        if (data > 0) {
                            $.messager.alert('注册提示', '帐号注册成功,等待管理员审核', 'warning',function () {
                                loginRegister.dialog('close');
                                login.dialog('open');
                            });
                        } else if (data == -1) {
                            $.messager.alert('注册提示', '帐号名称被占用', 'warning');
                        } else if (data == 0 ) {
                            $.messager.alert('注册提示', '未知错误，稍后重试...', 'warning');
                        }
                    }
                });
            }

        }
    }],
    onOpen : function () {
        $(function ()
        {
            $('#register-btn').parent().css('text-align', 'center');
        });
    }



});



//浏览器改变大小时触发
$(window).resize(function () {
    login.dialog('center');
});

//登录帐号验证
loginAccounts.textbox({
    width: 220,
    height: 32,
    required: true,
    iconCls: 'icon-man',
    validType: 'length[2,20]',
    missingMessage: '请输入登录帐号',
    invalidMessage: '登录帐号2到20位'
});
//登录密码验证
loginPassword.textbox({
    width: 220,
    height: 32,
    required: true,
    iconCls: 'icon-lock2',
    validType: 'length[6,30]',
    missingMessage: '请输入帐号密码',
    invalidMessage: '帐号密码6到30位'
});

//新增业务 帐号
btnRegister.click(function () {
    loginRegister.dialog('open');
    login.dialog('close');
});

//注册帐号验证
registerAccounts.textbox({
    width: 220,
    height: 32,
    required: true,
    iconCls: 'icon-man',
    validType: 'length[2,20]',
    missingMessage: '请输入注册帐号',
    invalidMessage: '登录帐号2到20位'
});
//注册密码验证
registerPassword.textbox({
    width: 220,
    height: 32,
    required: true,
    iconCls: 'icon-lock2',
    validType: 'length[6,30]',
    missingMessage: '请输入帐号密码',
    invalidMessage: '帐号密码6到30位'
});
//注册确认密码验证
registerNotPassword.textbox({
    width: 220,
    height: 32,
    required: true,
    iconCls: 'icon-lock2',
    validType: 'equals["#register-password"]',
    missingMessage: '请输入确认密码',
    invalidMessage: '确认密码和密码不一致'
});
//注册手机号码验证
registerTel.textbox({
    width: 220,
    height: 32,
    required: true,
    validType: 'length[11,11]',
    missingMessage: '请输入手机号码',
    invalidMessage: '手机号码11数字'
});

//检查密码和确认密码是否相同
$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function(value,param){
            return value == $(param[0]).val();
        },
        message: '确认密码和密码不一致'
    }
});
